package cn.itcast.service.impl;


import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.service.cargo.ContractService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service(timeout = 3000000) //调试要设置超时时间
public class ContractServiceImpl implements ContractService {
    //分页
    @Autowired
    private ContractDao contractDao;
    @Override
    public PageInfo<Contract> findByPage(ContractExample contractExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //查询
        List<Contract> list = contractDao.selectByExample(contractExample);

        return new PageInfo<>(list);
    }

    //查询全部
    @Override
    public List<Contract> findAll(ContractExample contractExample) {
        return contractDao.selectByExample(contractExample);
    }

    //根据id查询
    @Override
    public Contract findById(String id) {
        return contractDao.selectByPrimaryKey(id);
    }

    //保存
    @Override
    public void save(Contract contract) {
        //设置id
        contract.setId(UUID.randomUUID().toString());
        //初始化一些值
        //总金额
        contract.setTotalAmount(0d);
        //货物
        contract.setProNum(0);
        //附件数
        contract.setExtNum(0);
        //状态 0草稿 1已上报待报运	2 已报运
        contract.setState(0);
        //添加数据的时间
        contract.setCreateTime(new Date());
        contractDao.insertSelective(contract);

    }

    //更新
    @Override
    public void update(Contract contract) {
        contractDao.updateByPrimaryKeySelective(contract);
    }
    //根据id删除
    @Override
    public void delete(String id) {
        contractDao.deleteByPrimaryKey(id);

    }

    @Override
    public PageInfo<Contract> findContractByDeptId(String deptId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Contract> list = contractDao.findContractByDeptId(deptId);
        return new PageInfo<>(list);
    }
}
